import traceback

from PyQt5.QtCore import QDate
from PyQt5.QtWidgets import QMessageBox, QHeaderView

from base.base import BaseGui
from dao.rapid_country_db import RapidCountryManager
from enums.country_enum import CountryEnum
from rapid_api.crawler_country_total_info import get_country_total
from utils.button_tablewidget import WithButtonTableWidget
from utils.excel import ExportExcelDialog


class CountryGui(BaseGui):

    def __init__(self):
        super().__init__()

        self.country_page_size = 10  # 默认分页数
        self.country_page_num = 1  # 默认分页
        self.country_headers = CountryEnum.getDescList()
        self.countryTableWidget = self.ui.countryTableWidget
        self.country_page_data = None  # 当前页数据

        try:
            # 根据Country 搜索
            self.ui.countryPushButton.clicked.connect(self.indexCountry)
            # 分页展示第一页
            self.country_show_page(page_num=self.country_page_num)
            # 下一页
            self.ui.country_page_down.clicked.connect(self.country_page_down)
            # 上一页
            self.ui.country_page_up.clicked.connect(self.country_page_up)
            # 导出 country列表
            # self.ui.exportCountryPushButton.clicked.connect(self.exportCountrys)

        except Exception as e:
            traceback.print_exc()
            QMessageBox.about(self.ui, "账号页点击错误", str(e))

    def country_show_page(self, page_num):
        self.countryTableWidget.clear()
        self.country_page_data = None
        query_columns = CountryEnum.getValuesStr()
        start = (page_num - 1) * self.country_page_size
        rapidCountryManager = RapidCountryManager()
        self.country_page_data = rapidCountryManager.selectDataByPager(start, pageSize=self.country_page_size,
                                                                       query_columns=query_columns)
        WithButtonTableWidget(self, self.countryTableWidget, self.country_page_data, self.country_headers, 4)
        self.countryTableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)

    def country_page_down(self):
        self.country_page_num += 1
        self.country_show_page(self.country_page_num)

    def country_page_up(self):
        self.country_page_num -= 1
        if self.country_page_num == 0:
            self.country_page_num = 1
        self.country_show_page(self.country_page_num)

    def indexCountry(self):
        text = self.ui.countryLineEdit.text()
        if len(text) == 0:
            self.country_show_page(1)
            return
        rapidCountryManager = RapidCountryManager()
        query_columns = CountryEnum.getValuesStr()
        rapidCountry = rapidCountryManager.selectLikeInfoByCountry(text, query_columns)
        if rapidCountry is not None and len(rapidCountry) > 0:
            self.countryTableWidget.clear()
            WithButtonTableWidget(self, self.countryTableWidget, rapidCountry, self.country_headers, 4)
        else:
            result = get_country_total()
            if result is not None:
                rapidCountryManager1 = RapidCountryManager()
                print(result)
                rapidCountryManager1.batchInsertCountryByTuple(result, tuple(CountryEnum.getValuesList()))
            else:
                QMessageBox.about(self.ui, "error", f'当前Country 查无数据')
                return
        self.countryTableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)

    def exportCountrys(self):
        rapid = RapidCountryManager()
        start_time = QDate.currentDate().addDays(-3).toString("yyyy-MM-dd")  # 可拓展为页面选择
        end_time = QDate.currentDate().addDays(1).toString("yyyy-MM-dd")
        data = rapid.selectExportData(start_time, end_time)
        ExportExcelDialog(data)
        QMessageBox.about(self.ui, "导出国家信息", f'导出数据：{len(data)} 条')
